package com.shouxin.attendance.utils;

import android.app.Activity;
import android.content.DialogInterface;
import android.widget.Toast;

import com.shouxin.attendance.App;
import com.shouxin.attendance.view.CommonDialogWithText;

/**
 * <p>Toast提示</p>
 * Created by Jiang on 2015/10/14.
 */
public class UIUtils {

    private static Toast toast;
    private static CommonDialogWithText mTextDialog;

    public static void toast(String msg) {
        if (toast == null) {
            toast = Toast.makeText(App.getApp(), msg, Toast.LENGTH_SHORT);
        } else {
            toast.setText(msg);
        }
        toast.show();
    }

    public static void showLoading(Activity activity, String text) {
        showLoading(activity, true, text);
    }

    public static void showLoading(Activity activity, boolean cancel, String text) {
        showLoading(activity, cancel, text, null);
    }

    public static void showLoading(Activity activity, boolean cancel, String text, DialogInterface.OnDismissListener listener) {
        if (!activity.isFinishing()) {
            if (mTextDialog == null || activity != mTextDialog.getOwnerActivity()) {
                mTextDialog = new CommonDialogWithText(activity, text, cancel, listener);
            } else {
                mTextDialog.setText(text);
            }
            mTextDialog.show();
        }
    }

    public static void dismiss() {
        if (mTextDialog != null && mTextDialog.isShowing()) {
            mTextDialog.dismiss();
        }
    }
}
